<template>
	<view class="container">
		<form bindsubmit="createOrder">
			<view class="address-box">
				<view class="add-address" v-if="addressdefautl==null">
					<view class="title" @click="gopage('./address-add')">新增收货地址</view>
				</view>
				<view v-else class="show-address" @click="selectAddress">
					<view class="name-tel">{{addressdefautl.name}} {{addressdefautl.mobile}}</view>
					<view class="addr-text">{{addressdefautl.province}}{{addressdefautl.city}}{{addressdefautl.district}}{{addressdefautl.detail}}</view>
				</view>
			</view>
			<view class="uni-list" style="margin-bottom:20upx;">
				<radio-group @change="radioChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
						<view>
							<image src="/static/img/share/wx.png" mode="" style="width:80upx;height:80upx;vertical-align: middle;margin:0 20rpx;"></image>
							<text>{{item.name}}</text>
						</view>
						<view>
							<radio :value="item.value" :checked="index === current" />
						</view>
					</label>
				</radio-group>
			</view>
			<view class="goods-list">
				<view class="list-title">商品列表</view>
				<view class="a-goods" v-for="(item,index) in goodsList" :key="index">
					<view class="img-box">
						<image :src="baseURL+item.cover_pic" class="img" />
					</view>
					<view class="text-box">
						<view class="arow arow01">
							<view class="goods-name">{{item.g_name}}</view>
							<view class="goods-price">
								<text style="display:block;" v-if="p_config.store_price_view==2||p_config.store_price_view==3">{{item.company_title}}</text>
								<text style="display:block;" v-if="p_config.store_price_view==1">{{item.company_title}}</text>
								<text style="display:block;">¥ {{item.price}}</text>
							</view>
						</view>
						<view class="arow">
							<view class="goods-label"> </view><!-- item.label -->
							<view class="goods-num">x {{item.num}}</view>
							<!-- <view class="buy-num">
								<view class="jian-btn" :class="item.number==1? 'disabled' : ''" @click="jianBtnTap(index)">-</view>
								<input type="number" v-model="item.amount" disabled />
								<view class="jia-btn" :class="item.number==10? 'disabled' : ''" @click="jiaBtnTap(index)">+</view>
							</view> -->
						</view>
					</view>
				</view>
			</view>
			<view class="peisong-way">
				<view class="row-box">
					<view class="row-label">配送方式</view>
					<view class="right-text">快递</view>
					<!-- <view class="right-text" v-if="yunPrice == 0">包邮</view> -->
				</view>
				<!-- <view class="row-box">
					<view class="row-label">备注</view>
					<view class="right-text">
						<input name="remark" v-model="remark" type="text" class="liuyan" placeholder="如需备注请输入" />
					</view>
				</view> -->
			</view>
			<view class="goods-info">
				<view class="row-box" v-if="isNeedLogistics > 0 && yunPrice > 0">
					<view class="row-label">商品金额</view>
					<view class="right-text">¥ {{allGoodsPrice}}</view>
				</view>
				<view class="row-box" v-if="isNeedLogistics > 0 && yunPrice > 0">
					<view class="row-label">运费</view>
					<view class="right-text">+ ¥ {{yunPrice}}</view>
				</view>
				<view class="row-box">
					<view class="row-label">应付总额</view>
					<view class="right-text">¥ {{total_price}}</view><!-- {{allGoodsPrice+yunPrice}} -->
				</view>
			</view>
			<view class="jiesuan-box">
				<view class="left-price">
					<view class="total">合计：¥ {{total_price}}</view><!-- {{allGoodsPrice+yunPrice}} -->
				</view>
				<button class="to-pay-btn" @click="buyFromShoppingcar">立即支付</button>
			</view>
		</form>
	</view>
</template>

<script>
	import API from '@/common/api';
	// import CFG from '@/common/config';
	export default {
		data(){
			return {
				goodsList: [],
				baseURL:API.baseURL,
				total_price:0,//总价
				userid:uni.getStorageSync("userid"),
				addressdefautl:null,//默认地址
				ids:'',//购物车商品id （多）
			
				isNeedLogistics: 1, // 是否需要物流信息
				allGoodsPrice: 0,
				yunPrice: 0,
				goodsJsonStr: "",
				addres:'',//地址转换的文字
				remark:'',//备注
				cart_ids:[],//购物车商品id （多）
				p_config:{},
				num:0,
				not_car:false,
				
				current:1,
				items: [{
						value: 'alipay',
						name: '支付宝'
					},
					{
						value: 'wxpay',
						name: '微信',
						checked: 'true'
					}
				],
		}
		},
		onLoad: function(e) {
			this.goodsList = JSON.parse(e.goods);
			this.total_price = e.total_price;
			this.ids = e.id;
			// var data = JSON.parse(e.arr);
			// var len = data.length;
			// for (var i = 0; i < len; i++) {
			// 	this.shop_details({goods_id: data[i].ids},data[i].num);
			// }
			// var config = uni.getStorageSync('configData');
			// if(config){
			// 	this.p_config = JSON.parse(config);
			// 	console.log(this.p_config);
			// }
			// if(data[0].not_car){
			// 	this.not_car = true;
			// 	this.num = data[0].num;
			// }
		},
		onShow:function(e){
			this.address_defautl1();
		},
		methods: {
			radioChange(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						this.current = i;
						break;
					}
				}
			},
			address_defautl1(){//获取默认地址
				var _this=this;
				uni.request({
					url:API.mallAddress_list,
					method:"GET",
					data:{
						userid:_this.userid,
					},
					header:API.get_head(),
					success: (res) => {
						if(res.data.rows.length==0){return false}
						else{
							for (var i=0;i<res.data.rows.length;i++) {
								if(res.data.rows[i].is_default==1){
									_this.addressdefautl=res.data.rows[i];
									break;
								}
								if(i==res.data.rows.length-1){
									_this.addressdefautl=res.data.rows[0];
								}
							}
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					 }
				})
			},
			buyFromShoppingcar(){// 从购物车中提交订单
				var _this=this;
				uni.request({
					url:API.buyFromShoppingcar,
					method:"GET",
					data:{
						ids:_this.ids,
						userId:_this.userid,
						addressId:_this.addressdefautl.id,
					},
					header:API.get_head(),
					success: (res) => {
						if(res.data.success){
							_this.pay(parseFloat(res.data.pay_price),res.data.orderIds);
						}else{
							uni.showToast({
								title:res.data.message,
								icon:'none'
							})
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					 }
				})
			},
			pay(price,orderIds){
				var payType = this.items[this.current].value
				if(this.current==1){
					this.getOrderInfo(price);//微信支付
				}else{
					this.alipay(price,orderIds);//支付宝支付
				}
				
			},
			alipay(e,orderIds) {//获取支付信息
				let _this = this;                    
				// return new Promise((res) => {
					uni.request({
						url: API.appPay,
						method:"GET",
						data: {
							num: parseInt(e * 100),
							body:this.goodsList[0].guige?this.goodsList[0].guige:'',
							title:this.goodsList[0].g_name?this.goodsList[0].g_name:'',
							orderNum:orderIds,
						},
						success: (result) => {
							var orderInfo = result.data;
							if (orderInfo.code != 0) {
								uni.showModal({
									content: "获得订单信息失败",
									showCancel: false
								})
								this.loading = false;
								return;
							}
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: orderInfo.data,
								success: (e) => {
									uni.showModal({
										content: "支付成功",
										showCancel: false,
										success: () => {
											uni.navigateTo({
												url: '/pages/shop/SettlementResults?pay_price='+e+'&stu='+1
											});
										}
									})
								},
								fail: (e) => {
									console.log(JSON.stringify(e))
									uni.showModal({
										content: "支付失败",
										showCancel: false,
										success: () => {
											uni.navigateTo({
												url: '/pages/shop/SettlementResults?pay_price='+e+'&stu='+0
											});
										}
									})
								},
								complete: () => {
									this.loading = false;
								}
							})
						},
						fail: (e) => {
							// res(result)
						}
					})
				// })
			},
			getOrderInfo(e) {//获取支付信息
				let _this = this;
				// return new Promise((res) => {
					uni.request({
						url: API.wxpay,
						method:"GET",
						data: {
							pay_price: parseInt(e * 100),
						},
						success: (result) => {
							var orderInfo = result;
							if (orderInfo.statusCode != 200) {
								uni.showModal({
									content: "获得订单信息失败",
									showCancel: false
								})
								this.loading = false;
								return;
							}
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: orderInfo.data,
								success: (e) => {
									uni.showModal({
										content: "支付成功",
										showCancel: false,
										success: () => {
											uni.navigateTo({
												url: '/pages/shop/SettlementResults?pay_price='+e+'&stu='+1
											});
										}
									})
								},
								fail: (e) => {
									uni.showModal({
										content: "支付失败",
										showCancel: false,
										success: () => {
											uni.navigateTo({
												url: '/pages/shop/SettlementResults?pay_price='+e+'&stu='+0
											});
										}
									})
								},
								complete: () => {
									this.loading = false;
								}
							})
						},
						fail: (e) => {
							// res(result)
						}
					})
				// })
			},
			
			
			
			
			
			
			//更换地址
			selectAddress:function(){
				uni.navigateTo({
					url: 'select-address?sta=1',
				});
			},
			uporder:function(){
				if(this.addressdefautl==null){
					uni.showModal({content:"请选择收货地址",showCancel:false,});
					return false
				}
				if(this.not_car){
					this.buyone({
						goods_id:this.cart_ids.toString(),
						goods_number:this.num,
						address_id:this.addressdefautl.id,
						remark:this.remark,
					});
				}else{
					/*购买购物车中的*/
					this.shop_buycart({
						cart_ids:this.cart_ids.toString(),
						address_id:this.addressdefautl.id,
						remark:this.remark,
					})
				}
			},
			//单个商品下单
			async buyone(dataup) {
				const resData = await API.buyone(dataup);
				if(resData.code==200){
					let data = {
						order_sn:resData.data.order_sn,
						money:this.total_price,
					}
					uni.redirectTo({
						url:"./shop_pay?data="+JSON.stringify(data)
					})
				}
			},
			//购物车列表下单
			async shop_buycart(dataup,amount) {
				const resData = await API.shop_buycart(dataup);
				if(resData.code==200){
					let data = {
						order_sn:resData.data.order_sn,
						money:this.total_price,
					}
					uni.redirectTo({
						url:"./shop_pay?data="+JSON.stringify(data)
					})
				}
			},
			//获取商品信息
			async shop_details(dataup,amount) {
				const resData = await API.shop_details(dataup);
				if (resData.code == 200) {
					if(resData.data){
						resData.data['amount']=amount;
						this.goodsList.push(resData.data);
						this.cart_ids.push(resData.data.id);
						this.changeNum();
					}
				}
			},
			//获取默认地址
			async address_defautl(dataup) {
				const resData = await API.address_defautl(dataup);
				if(resData.code==200){
					if(resData.data.length!=0){
						this.addressdefautl=resData.data;
						this.addres='';
						var arr = resData.data.address_area.split(',');
						for(var i=0;i<CFG.CITY.length;i++){
							if(arr[0]==CFG.CITY[i].id){
								this.addres += CFG.CITY[i].name;//省
								for(var i1=0;i1<CFG.CITY[i].cityList.length;i1++){
									if(arr[1]==CFG.CITY[i].cityList[i1].id){
										this.addres += CFG.CITY[i].cityList[i1].name;//市
										if(arr.length==3){
											for(var i2=0;i2<CFG.CITY[i].cityList[i1].districtList.length;i2++){
												if(arr[2]==CFG.CITY[i].cityList[i1].districtList[i2].id){
													this.addres += CFG.CITY[i].cityList[i1].districtList[i2].name;//区
													// CFG.CITY[i].cityList[this.value[1]].districtList[this.value[2]].name;
													break;
												}
											}
										}
										break;
									}
								}
								break;
							}
						}
					}
				}
				
			},
			jiaBtnTap:function(index){
				this.goodsList[index]['amount']=this.goodsList[index]['amount']+1;
				this.changeNum();
			},
			jianBtnTap:function(index){
				if(this.goodsList[index]['amount']>1){
					this.goodsList[index]['amount']=this.goodsList[index]['amount']-1;
					this.changeNum();
				}
			},
			changeNum:function(){
				var len = this.goodsList.length;
				var crr_total_price=0;
				var config = uni.getStorageSync('configData');
				config = JSON.parse(config);
				for (var i=0;i<len;i++) {
					if(config.store_price_view==1){
						crr_total_price += parseInt(this.goodsList[i].amount)*(parseFloat(this.goodsList[i].point).toFixed(2))
					}else{
						crr_total_price += parseInt(this.goodsList[i].amount)*(parseFloat(this.goodsList[i].price).toFixed(2))
					}
				}
				this.total_price = crr_total_price;
			},
			gopage:function(e){
				if(e){
					uni.navigateTo({
						url:e,
					})
				}
			}
		}
	}
</script>

<style>
	/* @import "../../common/uni.css"; */

	.container {
		background-color: #F2f2f2;
		width:750upx;
		overflow-x:hidden;
	}

	.address-box {
		width: 100%;
		background: url(https://cdn.it120.cc/images/weappshop/arrow-right.png) no-repeat 705upx center ,
			url(https://cdn.it120.cc/images/weappshop/addr-line.png) no-repeat center bottom;
			background-size: 16upx auto, 750upx auto;
		background-color: #fff;
		margin: 20upx 0;
	}

	.add-addres {
		width: 100%;
	}

	.add-address .title {
		font-size: 28upx;
		color: #000;
		margin-left: 30upx;
		background: url(https://cdn.it120.cc/images/weappshop/ico-add-addr.png) no-repeat left center;
			background-size: 60upx auto;
		padding: 40upx 0 40upx 90upx;
	}

	.show-address {
		width: 664upx;
		box-sizing: border-box;
		padding-left: 100upx;
		background: url(https://cdn.it120.cc/images/weappshop/ico-addr.png) no-repeat 30upx 35upx;
			background-size: 30upx auto;
	}

	.show-address .name-tel {
		font-size: 28upx;
		color: #000;
		padding: 30upx 0 20upx 0;
	}

	.show-address .addr-text {
		font-size: 24upx;
		color: #888;
		padding-bottom: 34upx;
		line-height: 36upx;
	}

	form {
		width: 100%;
	}

	.goods-list {
		/* width: 100%; */
		background-color: #fff;
		margin-bottom: 20upx;
	}

	.goods-list .list-title {
		font-size: 28upx;
		color: #000;
		padding: 30upx 0 25upx 30upx;
	}

	.goods-list .a-goods {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		/*justify-content: space-between;*/
		border-top: 1upx solid #eee;
		padding: 30upx 30upx 30upx 0;
	}

	.goods-list .a-goods .img-box {
		width: 160upx;
		height: 160upx;
		overflow: hidden;
		margin-right: 20upx;
		background-color: #d8d8d8;
	}

	.goods-list .img-box .img {
		width: 160upx;
		height: 160upx;
	}

	.goods-list .a-goods .text-box {
		width: 510upx;
		box-sizing: border-box;
		padding-top: 10upx;
	}

	.a-goods .text-box .arow {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.a-goods .text-box .arow .goods-name {
		width: 360upx;
		font-size: 26upx;
		height: 74upx;
		color: #000000;
		line-height: 1.6;
		overflow: hidden;
	}

	.a-goods .text-box .arow01 {
		margin-bottom: 30upx;
	}

	.a-goods .text-box .arow .goods-price {
		font-size: 26upx;
		color: #000000;
		align-self: flex-start;
	}

	.a-goods .text-box .arow .goods-label {
		font-size: 26upx;
		color: #999;
	}

	.a-goods .text-box .arow .goods-num {
		font-size: 26upx;
		color: #999;
	}

	.peisong-way {
		width: 100%;
		background-color: #fff;
		margin-bottom: 20upx;
	}

	.peisong-way .row-box {
		width: 720upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 24upx 0 24upx 30upx;
		border-bottom: 1upx solid #eee;
	}

	.peisong-way .row-label {
		font-size: 28upx;
		color: #000;
	}

	.peisong-way .right-text {
		font-size: 28upx;
		color: #666;
		padding-right: 30upx;
	}

	.peisong-way .liuyan {
		width: 510upx;
		font-size: 28upx;
	}

	.goods-info {
		width: 100%;
		background-color: #fff;
		margin-bottom: 120upx;
		padding-bottom: 24upx;
	}

	.goods-info .row-box {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 24upx 30upx 12upx 30upx;
		font-size: 28upx;
		color: #000;
	}

	.goods-info .row-box .right-text {
		text-align: right;
	}

	.jiesuan-box {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 100upx;
		position: fixed;
		bottom: 0;
		left: 0;
		border-top: 1upx solid #eee;
		background-color: #fff;
	}

	.jiesuan-box .to-pay-btn {
		width: 250upx;
		text-align: center;
		height: 100%;
		line-height: 100upx;
		background-color: #e64340;
		font-size: 32upx;
		color: #ffffff;
		border-radius: 0;
		margin-top:0upx;
	}

	.jiesuan-box .left-price {
		display: flex;
		width: 500upx;
		justify-content: flex-end;
		line-height: 100upx;
		padding: 0 30upx 0 0;
		font-size: 28upx;
		box-sizing: border-box;
	}

	.jiesuan-box .total {
		color: #e64340;
		text-align: right;
		line-height:100upx;
	}



	/*数字选择框*/
	.buy-num {
			width: 164upx;
			height: 48upx;
			line-height: 48upx;
			display: flex;
			/*justify-content: space-between;*/
			font-size: 24upx;
			text-align: center;
		}
	
		.buy-num .jian-btn {
			width: 48upx;
			height: 100%;
			border-left: 1upx solid #ccc;
			border-bottom: 1upx solid #ccc;
			border-top: 1upx solid #ccc;
			border-bottom-left-radius: 6upx;
			border-top-left-radius: 6upx;
		}
	
		.buy-num .jian-btn.disabled {
			background-color: #f5f5f9;
			border-left: 1upx solid #eee;
			border-bottom: 1upx solid #eee;
			border-top: 1upx solid #eee;
			color: #ccc;
		}
	
		.buy-num .jia-btn {
			width: 48upx;
			height: 100%;
			border-right: 1upx solid #ccc;
			border-bottom: 1upx solid #ccc;
			border-top: 1upx solid #ccc;
			border-bottom-right-radius: 6upx;
			border-top-right-radius: 6upx;
		}
	
		.buy-num .jia-btn.disabled {
			background-color: #f5f5f9;
			border-right: 1upx solid #eee;
			border-bottom: 1upx solid #eee;
			border-top: 1upx solid #eee;
			color: #ccc;
		}
	
		.buy-num input {
			width: 68upx;
			height: 48upx;
			min-height: 48upx;
			text-align: center;
			font-size: 24upx;
			border: 1upx solid #ccc;
		}
</style>
